/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Scene;

import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

/**
 *
 * @author henriquediomede
 */
public class Terrain implements Drawable
{

    public void load(String fileName) 
    {
        try {
            FileInputStream fstream = new FileInputStream(fileName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            strLine = br.readLine();
            width = Integer.parseInt(strLine);

            strLine = br.readLine();
            height = Integer.parseInt(strLine);
            memoryAlloc();

            //int tempValue = 0;
            int i, j;
            i = 0;
            j = 0;

            Scanner scanner;
            printDimension();

            while ((strLine = br.readLine()) != null && i != height) {
                scanner = new Scanner(strLine);
                if (j == width) {
                    i++;
                    j = 0;
                }
                while (scanner.hasNextInt()) 
                {     
                    heightFieldMap[i][j] = scanner.nextInt();
                    j++;
                }

            }
            in.close();
      
        } catch (IOException ex) {
            Logger.getLogger(Terrain.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //printHeightFieldMap();
    }

    private void memoryAlloc() 
    {
        heightFieldMap = new int[height][width];
    }
    
    
    public void draw(GLAutoDrawable drawable) 
    {
         GL gl = drawable.getGL();
         gl.glCallList(dlTerrain);
    }
    
    public void init(GLAutoDrawable drawable)
    {
        GL gl = drawable.getGL();
       dlTerrain = gl.glGenLists(1);
       gl.glNewList(dlTerrain, GL.GL_COMPILE);
       terrain(drawable);
       gl.glEndList();
    }

    public void terrain(GLAutoDrawable drawable) 
    {

        GL gl = drawable.getGL();

        gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
        gl.glPushMatrix();
        gl.glTranslatef(-width*scaleFactor/2, 0, -height*scaleFactor/2);
        gl.glScalef(scaleFactor, scaleFactor, scaleFactor);

        for(int i = 0; i < height-1; i++)
        {
            gl.glBegin(GL.GL_TRIANGLE_STRIP);

            for(int j = 0; j < width; j++)
            {
                gl.glVertex3f(j, heightFieldMap[i][j], i);
                gl.glVertex3f(j, heightFieldMap[i+1][j], i+1);
              
            }


          gl.glEnd();
       }
        
        gl.glPopMatrix();

    }



    public void printDimension() 
    {
        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
    }

    public void printHeightFieldMap() 
    {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print(heightFieldMap[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    private int heightFieldMap[][];
    private int width;
    private int height;
    private int scaleFactor = 1;
    private int dlTerrain;

}
